﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.ESS.MODEL;
using KPIS.GERP.GFMS.APP.App_UI.Commons;

using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class MoneyRequest : System.Web.UI.UserControl
    {
        Main main = new Main();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();
        IBLL_FIM_MoneyRequestDtls mrd = new B_FIM_MoneyRequestDtls();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_FIM_MAS_BorrowTypes bt = new B_FIM_MAS_BorrowTypes();
        IBLL_BGM_MAS_ExpenseGroup expGrp = new B_BGM_MAS_ExpenseGroup();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void LoadMoneyRequest(string strMoneyReqSeq
            , string strBgmSourceSeq, string strBgmYear)
        {
            string strBgmMonth;
            //string strBgmProjOperSeq;

            lblMoneyReqSeq.Text = strMoneyReqSeq;
            lblBgmSourceSeq.Text = strBgmSourceSeq;
            lblBgmYear.Text = strBgmYear;
            LoadDDLBgmExpGrpSeq();

            if (strMoneyReqSeq == null || strMoneyReqSeq.Length == 0)
            {
                dpMoneyReqDate.SetValue(DateTime.Today);
                strBgmMonth = main.GetBgmMonth(strBgmSourceSeq, DateTime.Today).ToString();
                lblBgmProjOperSeq.Text = "";
                lblBgmProjOperCode.Text = "";
                lblBgmProjOperName.Text = "";
                lblRefType.Text = "เลขที่รายการเบิก :";
                lblRefNo.Text = "";
                txtMoneyReqName.Text = "";
                ddlBgmExpGrpSeq.SelectedIndex = 0;
            }
            else
            {
                M_FIM_MoneyRequests reqInfo = mr.GetInfo(strMoneyReqSeq);

                dpMoneyReqDate.SetValue(reqInfo.money_req_date);
                //chkUseAdvance.Checked = (reqInfo.use_advance == "1");
                rblUseAdvance.SelectedValue = reqInfo.use_advance;
                lblRefType.Text = "เลขที่รายการเบิก :";
                lblRefNo.Text = reqInfo.ref_no;
                txtMoneyReqName.Text = reqInfo.money_req_name;
                txtMoneyReqNamePlus.Text = reqInfo.money_req_name_plus;
                lblBgmProjOperSeq.Text = reqInfo.bgm_proj_oper_seq.ToString();
                lblBgmProjOperCode.Text = reqInfo.bgm_proj_oper_code;
                lblBgmProjOperName.Text = reqInfo.bgm_proj_oper_name;

                strBgmMonth = main.GetBgmMonth(strBgmSourceSeq, reqInfo.money_req_date).ToString();
                ddlBgmExpGrpSeq.SelectedValue = reqInfo.bgm_exp_grp_seq.ToString();
            }

            LoadRBLUseAdvance();
            MoneyRequestApprover1.LoadMoneyRequestApprover(strMoneyReqSeq);
            FIM_MoneyRequestDtls1.LoadRoot(strMoneyReqSeq, strBgmSourceSeq, strBgmYear
                , "12", lblBgmProjOperSeq.Text, ddlBgmExpGrpSeq.SelectedValue
                , Session["UserInternalJobsiteSeq"].ToString(), true);

            RadTabStrip1.SelectedIndex = 0;
            RadTabStrip1_TabClick(null, null);
        }

        protected void LoadDDLBgmExpGrpSeq()
        {
            ddlBgmExpGrpSeq.DataSource = expGrp.GetList(lblBgmSourceSeq.Text);
            ddlBgmExpGrpSeq.DataBind();

            ddlBgmExpGrpSeq.SelectedIndex = 0;
        }

        protected void LoadRBLUseAdvance()
        {
            string strBgmSourceSeq = lblBgmSourceSeq.Text;
            string strBgmSourceName = source.GetInfo(strBgmSourceSeq).bgm_source_name;

            rblUseAdvance.Items[0].Text = strBgmSourceName;
        }

        protected void ClearControl1()
        {
        }
        //=====================================================================

        //protected void CheckDateValid(object source, ServerValidateEventArgs args)
        //{
        //    if (!DateTimeX.IsValidDmyDate(args.Value, true) || args.Value == "")
        //    {
        //        args.IsValid = false;
        //    }
        //}

        protected void rblBorrowTypeSeq_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetBorrowReturnDate();
        }

        protected void txtBorrowEndDate_TextChanged(object sender, EventArgs e)
        {
            GetBorrowReturnDate();
        }

        protected void GetBorrowReturnDate()
        {
            
        }

        public bool Insert()
        {
            bool bOK = true;
            M_FIM_MoneyRequests info = new M_FIM_MoneyRequests();
            M_MessageSend sendInfo = new M_MessageSend();
            string strMoneyReqSeq;
            string strBgmSourceSeq = lblBgmSourceSeq.Text;
            string strBgmYear = lblBgmYear.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;
            string strEventSeq = "16";
            string strUseAdvance = rblUseAdvance.SelectedValue;
            string strMoneyReqDate = dpMoneyReqDate.GetStringValue();
            string strMoneyReqName = txtMoneyReqName.Text;
            string strMoneyReqNamePlus = txtMoneyReqNamePlus.Text;
            string strRefNo = lblRefNo.Text;

            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.work_area_seq = main.ConvertNullInt(strWorkAreaSeq);
            info.event_seq = main.ConvertNullInt(strEventSeq);
            info.use_advance = strUseAdvance;
            info.money_req_date = main.ConvertNullDateTime(strMoneyReqDate);
            info.money_req_name = strMoneyReqName;
            info.money_req_name_plus = strMoneyReqNamePlus;
            info.ref_no = strRefNo;
            info.approver_seq = null;
            info.emp_seq = main.ConvertNullInt(strUserID);
            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = main.ConvertNullDateTime(dtNow);
            info.advance_bgm_source_seq = null;

            strMoneyReqSeq = mr.Insert(info);

            FIM_MoneyRequestDtls1.Insert(strMoneyReqSeq);

            //================================================================

            ClearControl1();
            return bOK;
        }

        public bool Update()
        {
            bool bOK = true;
            M_FIM_MoneyRequests info = new M_FIM_MoneyRequests();
            M_MessageSend sendInfo = new M_MessageSend();
            string strMoneyReqSeq = lblMoneyReqSeq.Text;
            string strBgmSourceSeq = lblBgmSourceSeq.Text;
            string strBgmYear = lblBgmYear.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;
            string strUseAdvance = rblUseAdvance.SelectedValue;
            string strMoneyReqDate = dpMoneyReqDate.GetStringValue();
            string strMoneyReqName = txtMoneyReqName.Text;
            string strMoneyReqNamePlus = txtMoneyReqNamePlus.Text;
            string strRefNo = lblRefNo.Text;

            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.money_req_seq = Convert.ToInt32(strMoneyReqSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.money_req_date = main.ConvertNullDateTime(strMoneyReqDate);
            info.event_seq = 16;
            info.use_advance = strUseAdvance;
            info.money_req_name = strMoneyReqName;
            info.money_req_name_plus = strMoneyReqNamePlus;
            info.ref_no = strRefNo;
            info.emp_seq = main.ConvertNullInt(strUserID);
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);
            info.advance_bgm_source_seq = null;

            mr.Update(info);

            FIM_MoneyRequestDtls1.Update(strMoneyReqSeq);
            //================================================================

            ClearControl1();
            return bOK;
        }

        protected void ddlBgmExpGrpSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            FIM_MoneyRequestDtls1.LoadRoot(lblMoneyReqSeq.Text, lblBgmSourceSeq.Text
                , lblBgmYear.Text, "12", lblBgmProjOperSeq.Text, ddlBgmExpGrpSeq.SelectedValue
                , Session["UserInternalJobsiteSeq"].ToString(), true);
        }

        protected void RadTabStrip1_TabClick(object sender, Telerik.Web.UI.RadTabStripEventArgs e)
        {
            switch (RadTabStrip1.SelectedIndex)
            {
                case 0:
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                    break;
                case 1:
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                    break;
            }
        }
    }
}